package com.wanmait.schedule.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanmait.schedule.entity.CourseTeacher;
import com.wanmait.schedule.entity.Permission;
import com.wanmait.schedule.entity.Role;
import com.wanmait.schedule.entity.RolePermission;
import com.wanmait.schedule.mapper.PermissionMapper;
import com.wanmait.schedule.mapper.RoleMapper;
import com.wanmait.schedule.mapper.RolePermissionMapper;
import com.wanmait.schedule.service.RolePermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 角色权限表 服务实现类
 * </p>
 *
 * @author wanmait
 * @since 2022-12-05
 */
@Service
public class RolePermissionServiceImpl extends ServiceImpl<RolePermissionMapper, RolePermission> implements RolePermissionService {

    @Resource
    private RoleMapper roleMapper;
    @Resource
    private PermissionMapper permissionMapper;
    @Resource
    private RolePermissionMapper rolePermissionMapper;

    @Override
    public PageInfo<Role> list(RolePermission rolePermission, Integer pageNum) {
        List<Role> roles = new ArrayList<>();
        if(rolePermission.getPermissionId()!=null || rolePermission.getRoleId()!=null){
            LambdaQueryWrapper<RolePermission> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(rolePermission.getPermissionId()!=null,RolePermission::getPermissionId,rolePermission.getPermissionId());
            queryWrapper.eq(rolePermission.getRoleId()!=null,RolePermission::getRoleId,rolePermission.getRoleId());
            List<RolePermission> rolePermissions = rolePermissionMapper.selectList(queryWrapper);
            if(rolePermissions.isEmpty()){
                List<Role> roleList = new ArrayList<>();
                PageInfo<Role> pageInfo = new PageInfo<>(roleList,9);
                return pageInfo;
            }
            Set<Integer> roleList = new HashSet<>();
            for(RolePermission r: rolePermissions){
                roleList.add(r.getRoleId());
            }
            Integer[] rolel = roleList.toArray(new Integer[0]);
            PageHelper.startPage(pageNum,6);
            LambdaQueryWrapper<Role> roleQueryWrapper = new LambdaQueryWrapper<>();
            roleQueryWrapper.in(Role::getId,rolel);
            roles = roleMapper.selectList(roleQueryWrapper);
        }
        else {
            PageHelper.startPage(pageNum,6);
            roles = roleMapper.selectList(null);
        }
        for(Role r:roles){
            LambdaQueryWrapper<RolePermission> rolePermissionWrapper = new LambdaQueryWrapper<>();
            rolePermissionWrapper.in(RolePermission::getRoleId,r.getId());
            List<RolePermission> rolePermissions = rolePermissionMapper.selectList(rolePermissionWrapper);
            List<Integer> permissionList = new ArrayList<>();
            for(RolePermission rp : rolePermissions){
                permissionList.add(rp.getPermissionId());
            }
            r.setPermissionIds(permissionList);
        }
        if(pageNum == null){
            pageNum = 1;
        }
        PageInfo<Role> pageInfo = new PageInfo<>(roles,9);
        return pageInfo;
    }

    @Override
    public List<Permission> listByRoleId(Integer id) {
        List<Permission> permissions = permissionMapper.listByRoleId(id);
        return permissions;
    }

    @Override
    public void update(Integer id, Integer[] ids) {
        LambdaQueryWrapper<RolePermission> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RolePermission::getRoleId,id);
        List<RolePermission> rolePermissions = rolePermissionMapper.selectList(queryWrapper);
        //删除
        for(RolePermission rolePermission : rolePermissions){
            Boolean flag = true;
            for(Integer i : ids){
                if(i == rolePermission.getPermissionId()){
                    flag=false;
                    break;
                }
            }
            if(flag){
                LambdaQueryWrapper<RolePermission> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(RolePermission::getRoleId,id);
                wrapper.eq(RolePermission::getPermissionId,rolePermission.getPermissionId());
                rolePermissionMapper.delete(wrapper);
            }
        }
        //添加
        for(Integer i : ids){
            Boolean flag=true;
            for(RolePermission rolePermission : rolePermissions){
                if(i == rolePermission.getPermissionId()){
                    flag=false;
                    break;
                }
            }
            if(flag){
                RolePermission rp = new RolePermission();
                rp.setRoleId(id);
                rp.setPermissionId(i);
                rolePermissionMapper.insert(rp);
            }
        }
    }
}
